
'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');
const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));

module.exports.stucurList=(req,res)=>{

  const page=parseInt(req.query.page||'1');
  const limit=parseInt(req.query.limit||'12');
  const start=(page-1)*limit;

  db.table('course').count("*").then((count)=>{
   const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
   const totalPageArray=[];
   for(var i=1;i<=totalPage;i++){

   	  totalPageArray.push(i);
   }

   db.table('course').order('course_id DESC').limit(start,limit).select().then((data)=>{

      xtpl.renderFile(
        path.join(__dirname,"../views/admin/stucur_list.html"),
        {

      array:data,
      totalPage:totalPage,
      totalPageArray:totalPageArray

        },
        (err,content)=>{

        	if(err){
        		return console.log(err);
        	}

        	res.setHeader("Content-Type", "text/html;charset=utf-8");
        	res.end(content);
        }
   	);
  

   });



  });
};


module.exports.stucurAdd=(req,res)=>{


    let course_id=req.params.course_id||0;
    let where=`course_id=${course_id}`;


    db.table('course').where(where).find().then((data)=>{

          
    xtpl.renderFile(

       path.join(__dirname,"../views/admin/stucur_add.html"),
       {
         
         course:data

       },
       (err,content)=>{
           
            if(err){
            	return console.log(err);
            }

            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end(content);

       }

     	);
    });
};


module.exports.stucurAddData=(req,res)=>{
 
  let where=`indent_name='${req.body.indent_name}'`;
  db.table('indent').where(where).find().then((data)=>{

        if(JSON.stringify(data)!="{}"){
        
           res.setHeader("Content-Type","text/html;charset=utf-8");
           res.end("<script>alert('该课程已经存在，请重新购买');location.href='/stucur/stucur_list';</script>");
           return false; 

        }else{
           
           let data={
           
              "indent_name":req.body.indent_name,
              "indent_price":req.body.indent_price,
              "indent_time":(Date.parse(moment(req.body.indent_time))/1000),

           };
           db.table('indent').add(data).then((inserId)=>{

              res.redirect('/stucur/stucur_list');
              return false;

           }).catch((err)=>{

           	  if(err){
           	  	return console.log(err);
           	  }
           });





        }

  });


};


module.exports.stuindList=(req,res)=>{



const page=parseInt(req.query.page||'1');
const limit=parseInt(req.query.limit||'12');

const start=(page-1)*limit;

db.table('indent').count("*").then((count)=>{

  const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
  const totalPageArray=[];
  for(var i=1;i<=totalPage;i++){
    totalPageArray.push(i);
  };
 
  db.table('indent').select().then((data)=>{
       

   data.indent_time=moment(data.indent_time*1000).format("YYYY-MM-DD");
    xtpl.renderFile(

     path.join(__dirname,'../views/admin/stuind_list.html'),
     {

     	indent:data,
     	totalPage:totalPage,
      totalPageArray:totalPageArray,
      page:page
     },
     (err,content)=>{
     	if(err){
     		return console.log(err);
     	}
     	res.setHeader("Content-Type","text/html;charset=utf-8");
     	res.end(content);
     }

  	);
     

 });




});






};
